<template>
  <el-space size="large" direction="vertical" style="width: 100%;margin-top: 100px">

    <el-form
      ref="ruleFormRef"
      :model="ruleForm"
      status-icon
      :rules="rules"
      label-width="120px"
      class="demo-ruleForm"
    >
      <el-form-item label="原密码" prop="oldPwd">
        <el-input v-model.number="ruleForm.oldPwd" />
      </el-form-item>
      <el-form-item label="新密码" prop="pass">
        <el-input v-model="ruleForm.pass" type="password" autocomplete="off" />
      </el-form-item>
      <el-form-item label="确认密码" prop="checkPass">
        <el-input
          v-model="ruleForm.checkPass"
          type="password"
          autocomplete="off"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="submitForm(ruleFormRef)"
        >Submit
        </el-button
        >
        <el-button @click="resetForm(ruleFormRef)">Reset</el-button>
      </el-form-item>
    </el-form>
  </el-space>
</template>

<script lang="ts" setup>
import {reactive, ref} from 'vue'
import type {FormInstance} from 'element-plus'
import {userApi} from "@/api/user";
import {ElNotification} from "element-plus/es";
import {router} from "@/router";

const ruleFormRef = ref<FormInstance>()

const checkAge = (rule: any, value: any, callback: any) => {
  if (!value) {
    return callback(new Error('Please input the age'))
  } else {
    callback()
  }

}

const validatePass = (rule: any, value: any, callback: any) => {
  if (value === '') {
    callback(new Error('Please input the password'))
  } else {
    if (ruleForm.checkPass !== '') {
      if (!ruleFormRef.value) return
      ruleFormRef.value.validateField('checkPass', () => null)
    }
    callback()
  }
}
const validatePass2 = (rule: any, value: any, callback: any) => {
  if (value === '') {
    callback(new Error('Please input the password again'))
  } else if (value !== ruleForm.pass) {
    callback(new Error("Two inputs don't match!"))
  } else {
    callback()
  }
}

const ruleForm = reactive({
  pass: '',
  checkPass: '',
  oldPwd: '',
})

const rules = reactive({
  pass: [{validator: validatePass, trigger: 'blur'}],
  checkPass: [{validator: validatePass2, trigger: 'blur'}],
  oldPwd: [{validator: checkAge, trigger: 'blur'}],
})

const submitForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.validate((valid) => {
    if (valid) {

      userApi.changePassword({oldPwd:ruleForm.oldPwd,newPwd:ruleForm.checkPass}).then(res=>{
        const {code,msg}=res.data
        if (code==200){
          ElNotification({
            title: '修改成功',
            message: msg,
            type: 'success',
          })
          router.back()
        }else {
          ElNotification({
            title: '修改失败',
            message: msg,
            type: 'error',
          })
        }
      })

    } else {
      console.log('error submit!')
      return false
    }
  })
}

const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.resetFields()
}
</script>


<!--<template>-->
<!--  <el-space size="large" direction="vertical" style="width: 100%;margin-top: 100px">-->
<!--    <el-form-->
<!--      ref="ruleFormRef"-->
<!--      :model="ruleForm"-->
<!--      status-icon-->
<!--      :rules="rules"-->
<!--      label-width="120px"-->
<!--      class="demo-ruleForm"-->
<!--    >-->
<!--      <el-form-item label="原密码" prop="oldPwd">-->
<!--        <el-input v-model="ruleForm.oldPwd" type="password" autocomplete="off" />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="新密码" prop="newPwd1">-->
<!--        <el-input v-model="ruleForm.newPwd1" type="password" autocomplete="off" />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="确认密码" prop="newPwd2">-->
<!--        <el-input v-model.number="ruleForm.newPwd2" type="password" autocomplete="off" />-->
<!--      </el-form-item>-->
<!--      <el-form-item>-->
<!--        <el-button type="primary" @click="submitForm(ruleFormRef)"-->
<!--        >确认修改-->
<!--        </el-button-->
<!--        >-->
<!--        <el-button @click="resetForm(ruleFormRef)">重置</el-button>-->
<!--      </el-form-item>-->
<!--    </el-form>-->
<!--  </el-space>-->

<!--</template>-->

<!--<script lang="ts" setup>-->
<!--import {reactive, ref} from 'vue'-->
<!--import type {FormInstance} from 'element-plus'-->

<!--const ruleFormRef = ref<FormInstance>()-->

<!--const checkOldPwd = (rule: any, value: any, callback: any) => {-->
<!--  if (value === '') {-->
<!--    callback(new Error('请输入新密码'))-->
<!--  } else {-->
<!--    callback()-->
<!--  }-->
<!--}-->

<!--const validatePass = (rule: any, value: any, callback: any) => {-->
<!--  if (value === '') {-->
<!--    callback(new Error('请输入新密码'))-->
<!--  } else {-->
<!--    if (ruleForm.newPwd1 !== '') {-->
<!--      if (!ruleFormRef.value) return-->
<!--      ruleFormRef.value.validateField('newPwd1', () => null)-->
<!--    }-->
<!--    callback()-->
<!--  }-->
<!--}-->
<!--const validatePass2 = (rule: any, value: any, callback: any) => {-->
<!--  if (value === '') {-->
<!--    callback(new Error('请输入确认密码'))-->
<!--  } else if (value !== ruleForm.newPwd2) {-->
<!--    callback(new Error("两次密码不一致"))-->
<!--  } else {-->
<!--    callback()-->
<!--  }-->
<!--}-->

<!--const ruleForm = reactive({-->
<!--  oldPwd: '',-->
<!--  newPwd1: '',-->
<!--  newPwd2: '',-->
<!--})-->

<!--const rules = reactive({-->
<!--  newPwd1: [{validator: validatePass, trigger: 'blur'}],-->
<!--  newPwd2: [{validator: validatePass2, trigger: 'blur'}],-->
<!--  oldPwd: [{validator: checkOldPwd, trigger: 'blur'}],-->
<!--})-->

<!--const submitForm = (formEl: FormInstance | undefined) => {-->
<!--  if (!formEl) return-->
<!--  formEl.validate((valid) => {-->
<!--    if (valid) {-->
<!--      console.log('提交')-->
<!--    } else {-->
<!--      console.log('error submit!')-->
<!--      return false-->
<!--    }-->
<!--  })-->
<!--}-->

<!--const resetForm = (formEl: FormInstance | undefined) => {-->
<!--  if (!formEl) return-->
<!--  formEl.resetFields()-->
<!--}-->
<!--</script>-->
